#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "yarn_before.h"
#include "com.h"
#include "mhlabel.h"
#include "keypane.h"
#include "mh/common.h"
#include "run.h"
#include "info.h"
#include "mhctrlstate.h"

#define START_WIDTH       77
#define START_HEIGHT      34
#define START_EDIT_WIDTH        30
#define START_LABLE_WIDTH    20
#define START_POINT_CAPTION_LEFT        (CX_EDGE_LEFT * 2)
#define START_POINT_CAPTION_TOP         (CY_TITLE4_HEIGHT + CX_EDGE_LEFT * 2 - 25)
#define PAGE_NUM 12
/*
 *
 */
static void CreateYarnBeforeInterface(HWND hWnd)
{
    char sc[256] = {0};
    int i = 0, j = 0;
    //sys btn
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_XTCS_WND_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);

    for(i = 0; i < 10; i++)
    {
        for(j = 0; j < 10; j ++)
        {
            sprintf(sc, "%u", i*10 + j + 1);
            CreateWindow (MH_LABEL_NAME,
                sc,
                WS_CHILD | WS_VISIBLE | BS_LEFT,
                IDC_STATIC,
                START_POINT_CAPTION_LEFT + i * START_WIDTH,
                START_POINT_CAPTION_TOP  + j*START_HEIGHT,
                START_LABLE_WIDTH,
                START_HEIGHT,
                hWnd, MH_LABEL_COLOR_1GROUP);
            sprintf(sc, "%u", GetYarnBeforeSet(g_knit_data, 0, i*10 + j));
            CreateWindow (MH_EDIT_NAME,
                sc,
                WS_CHILD | WS_VISIBLE,
                IDC_EDIT_YARN_SET + i*10 + j,
                START_POINT_CAPTION_LEFT + i*START_WIDTH +30,
                START_POINT_CAPTION_TOP+ j*START_HEIGHT,
                START_EDIT_WIDTH,
                START_HEIGHT,
                hWnd, 0);
        }
    }
    int yarn_right[6] = {340, 420, 490, 560, 630, 700};
    const char *p = GetString(874);
    int yarn_id[6] = {ID_YARN_ALL_COPY, ID_YARN_ALL_ADD, ID_YARN_ALL_MIN, ID_YARN_COPY, ID_YARN_LAST, ID_YARN_NEXT};
    for(i = 0; i < 6; ++i)
    {
        p = GetString(874 + i);
        MH_CTRL_STATE *ctrl = NEW_MH_CTRL_STATE();
        ctrl->type = MH_CTRL_TYPE_CAM;
        ctrl->state = MH_CTRL_STATE_NORM;
        ctrl->mode |= MH_CTRL_CLICK_DISABLE;
        CreateWindow(MH_CTRL_STATE_NAME,
            p,
            WS_CHILD | WS_VISIBLE,
            yarn_id[i],
            yarn_right[i] + 20,
            400,
            i ? 68 : 78,
            30,
            hWnd, (int)ctrl);
    }
    CreateWindow(MH_LABEL_NAME,
        GetString(880),
        WS_CHILD | WS_VISIBLE | BS_LEFT,
        IDC_TIP,
        30,
        400,
        200,
        30,
        hWnd, MH_LABEL_COLOR_4GROUP);
}
/*
 *把界面内有变更的数据传给g_knit_data
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    int index = nID - IDC_EDIT_YARN_SET;
    uchar n = atoi(sc);
    SetYarnBeforeSet(g_knit_data, data.yarn.page, index, n);
    data.yarn.all = TRUE;

    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *界面下方的警告提醒
 */
static void OnChangeCtrl(HWND hWnd, uchar page)
{
    WND_EXTRA_DATA data;
    HWND hTmp;
    data.value = GetWindowAdditionalData(hWnd);
    const char *sc = GetString(880 + page);

    hTmp = GetDlgItem(hWnd, IDC_TIP);
    SetWindowCaption(hTmp, sc);
    InvalidateRect(hTmp, NULL, TRUE);
}
/*
 *
 */
 static void SetEditFocusId(HWND hWnd)
 {
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(GetDlgCtrlID(GetFocusChild(hWnd)) < ID_YARN_ALL_COPY)
    {
        data.yarn.focus_id = GetDlgCtrlID(GetFocusChild(hWnd));
    }
   SetWindowAdditionalData(hWnd, data.value);
 }
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
	switch(nID)
	{
	case ID_ESC:
	    {
	      //  if(data.yarn.all)
            {
                printf("data:%d\n", data.yarn.all);
                printf("write data to sd：%s\n",MAC_PARAM_FILE);
                int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
                if(ret)
                {
                    printf("write param: %d\n", ret);
                    AfxInfoBox(hWnd, NULL, GetString(481));
                }
                else
                {
                    MH_FILE_SYNC();
                }
            }
            SendBeforeCom(g_knit_data);
            data.yarn.all = FALSE;
	    	PostMessage (hWnd, MSG_CLOSE, 0, 0);
	    }
		break;
    case ID_YARN_ALL_COPY:
        {
            data.yarn.all = TRUE;
            int src_page = data.yarn.page;
            int order[PAGE_NUM] = { 2, 3, 0, 1, 5, 4, 7, 6, 9, 8, 11, 10 };
            int des_page = order[src_page];
            YarnPageDataCopy(g_knit_data, des_page, src_page);
            SendBeforeData(g_knit_data, 100, des_page * 100);
            AfxInfoBox(hWnd, GetString(390), GetString(926));
        }
        break;
    case ID_YARN_ALL_ADD:
        {
            int i = 0, j = 0;
            char sc[5] = "";
            short num = 0;
            for(i = 0; i < 10; i++)
            {
                for(j = 0; j < 10; j++)
                {
                    HWND hTwd = GetDlgItem(hWnd, IDC_EDIT_YARN_SET + i*10 +j);
                    num = atoi(GetWindowCaption(hTwd)) + 1;
                    SetYarnBeforeSet(g_knit_data, data.yarn.page, i*10 +j, num);
                    sprintf(sc, "%d", num);
                    SetWindowCaption(hTwd, sc);
                    InvalidateRect(hTwd, NULL, TRUE);
                }
            }
            SendBeforeData(g_knit_data, 100, data.yarn.page*100);
            data.yarn.all = TRUE;
        }
        break;
    case ID_YARN_ALL_MIN:
        {
            int i = 0, j = 0;
            char sc[5] = "";;
            short num = 0;
            for(i = 0; i < 10; i++)
            {
                for(j = 0; j < 10; j++)
                {
                    HWND hTwd = GetDlgItem(hWnd, IDC_EDIT_YARN_SET + i*10 +j);
                    num = atoi(GetWindowCaption(hTwd)) - 1;
                    SetYarnBeforeSet(g_knit_data, data.yarn.page, i*10 +j, num);
                    sprintf(sc, "%d", num);
                    SetWindowCaption(hTwd, sc);
                    InvalidateRect(hTwd, NULL, TRUE);
                }
            }
            SendBeforeData(g_knit_data, 100, data.yarn.page*100);
            data.yarn.all = TRUE;
        }
        break;
    case ID_YARN_COPY:
        {
            int delt =  data.yarn.focus_id - IDC_EDIT_YARN_SET;
            int index = delt%10;
            HWND wnd = GetDlgItem(hWnd, data.yarn.focus_id);
            const char *sc = GetWindowCaption(wnd);
            int i = 0;
            for(i = 1; i < 10-index; i++)
            {
                SetYarnBeforeSet(g_knit_data, data.yarn.page, delt + i, atoi(sc));
                wnd = GetDlgItem(hWnd, data.yarn.focus_id + i);
                SetWindowCaption(wnd, sc);
                InvalidateRect(wnd, NULL, TRUE);
            }

            SendBeforeData(g_knit_data, 10 - index, data.yarn.page*100 + delt);
            data.yarn.all = TRUE;
        }
        break;
    case ID_YARN_NEXT:
	    {
            if(data.yarn.page <11)
            {
                data.yarn.page++;
                short num = 0;
                int i = 0, j = 0;
                char sc[5] = {"0"};
                for(i = 0; i < 10; i++)
                {
                    for(j = 0; j < 10; j++)
                    {
                        HWND hTwd = GetDlgItem(hWnd, IDC_EDIT_YARN_SET + i*10 +j);
                        num = GetYarnBeforeSet(g_knit_data, data.yarn.page, i*10 +j);
                        sprintf(sc, "%d", num);
                        SetWindowCaption(hTwd, sc);
                        InvalidateRect(hTwd, NULL, TRUE);
                    }
                }
                GetYarnBeforeSet(g_knit_data, data.yarn.page, i*10 +j);
                OnChangeCtrl(hWnd, data.yarn.page);
            }
	    }
	    break;
    case ID_YARN_LAST:
	    {
	      if(data.yarn.page != 0)
            {
                data.yarn.page--;
                short num = 0;
                int i = 0, j = 0;
                char sc[5] = {0};
                for(i = 0; i < 10; i++)
                {
                    for(j = 0; j < 10; j++)
                    {
                        HWND hTwd = GetDlgItem(hWnd, IDC_EDIT_YARN_SET + i*10 +j);
                        num = GetYarnBeforeSet(g_knit_data, data.yarn.page, i*10 +j);
                        sprintf(sc, "%d", num);
                        SetWindowCaption(hTwd, sc);
                        InvalidateRect(hTwd, NULL, TRUE);
                    }
                }
            }
            OnChangeCtrl(hWnd, data.yarn.page);
	    }
	    break;
    default:
        {
            HWND hTmp = GetDlgItem(hWnd, nID);
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);

            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            int value = atoi(GetWindowCaption(hTmp));
            CreateKeyboardWindow(hWnd, rect, -500, 500, value);
        }
        break;
	}

	SetWindowAdditionalData(hWnd, data.value);
}

static void OnKeyDown(HWND hWnd, uint key)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    int index = data.yarn.focus_id -IDC_EDIT_YARN_SET;

	if(key == SCANCODE_ESCAPE)
	{
		OnBtnCmd(hWnd, ID_ESC);
	}
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = 0;
        if(index >= 10)
        {
            nID = data.yarn.focus_id - 10;
        }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = 0;
        if(index < 90)
        {
            nID = data.yarn.focus_id + 10;
        }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = data.yarn.focus_id - 1;
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = data.yarn.focus_id +1;
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        OnBtnCmd(hWnd, nID);
    }
    else if(key == SCANCODE_A)
    {
        OnBtnCmd(hWnd, ID_YARN_ALL_COPY);
    }
    else if(key == SCANCODE_B)
    {
        OnBtnCmd(hWnd, ID_YARN_ALL_ADD);
    }
    else if(key == SCANCODE_C)
    {
        OnBtnCmd(hWnd, ID_YARN_ALL_MIN);
    }
    else if(key == SCANCODE_D)
    {
        OnBtnCmd(hWnd, ID_YARN_COPY);
    }
    else if(key == SCANCODE_E)
    {
        OnBtnCmd(hWnd, ID_YARN_LAST);
    }
    else if(key == SCANCODE_F)
    {
        OnBtnCmd(hWnd, ID_YARN_NEXT);
    }

}
/*
 *
 */
static int YarnBeforeWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
    {
    case MSG_CREATE:
        {
        	CreateYarnBeforeInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            memset(&data.yarn, 0, sizeof(WND_YARN_SET));
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 200;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 200;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
    	{
    	    OnKeyDown(hWnd, wParam);
    	    SetEditFocusId(hWnd);
    	}
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_WORK]);
        }
    	return 0;
    case MSG_COMMAND:
        {
        	OnBtnCmd(hWnd, LOWORD(wParam));
        	SetEditFocusId(hWnd);
        }
    	return 0;
    case MSG_SETDATA:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            int delt =  data.yarn.focus_id - IDC_EDIT_YARN_SET;
            OnSetKnitValue(hWnd, LOWORD(wParam));
            SendBeforeData(g_knit_data, 1, data.yarn.page * 100 + delt);
            SetWindowAdditionalData(hWnd, data.value);
            return 0;
        }
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateYarnBeforeWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_XTCS_WND_BMP) / 2;
    int y = (CY_SCREEN - CY_XTCS_WND_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_YARN_BEFORE_SET;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = YarnBeforeWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_XTCS_WND_BMP;
    CreateInfo.by = y + CY_XTCS_WND_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT_YARN_SET));
        SetEditFocusId(hWnd);
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }

    return hWnd;
}
